﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Security.Cryptography;
using System.Text;

/// <summary>
/// Summary description for Tool
/// </summary>
public class Tool
{
    public static string Capcha()
    {
        Random a = new Random();
        return a.Next().ToString().Substring(0, 5);
    }
    public static string MaHoa(string a) // ma hoa
    {
        try
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] chuoi = Encoding.ASCII.GetBytes(a);
            byte[] mahoa = md5.ComputeHash(chuoi);
            return Convert.ToBase64String(mahoa).Trim();
        }
        catch
        {
            return null;
        }
    }
    public static string MaNgauNhien_So(int codeCount)
    {
        string allChar = "0,1,2,3,4,5,6,7,8,9";
        string[] allCharArray = allChar.Split(',');
        string randomCode = "";
        int temp = -1;

        Random rand = new Random();
        for (int i = 0; i < codeCount; i++)
        {
            if (temp != -1)
            {
                rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
            }
            int t = rand.Next(10);
            if (temp != -1 && temp == t)
            {
                return MaNgauNhien_So(codeCount);
            }
            temp = t;
            randomCode += allCharArray[t];
        }
        return randomCode;
    }
    public static int MenuId = -1;
    public static int DataId = 0;

    public static string XulyIcon(string kytu)
    {
        if (kytu.Length == 2)
            kytu += "&&";
        else if (kytu.Length == 3)
            kytu += "&";
        return kytu;
    }

    public static string XulyComment(string txtnoidung)
    {
        string[] chuoi = txtnoidung.Split('\n');
        string nd = string.Empty;
        for (int i = 0; i < chuoi.Count(); i++)
        {
            nd += chuoi[i] + "<br>";
        }
        string noidung = nd;
        //foreach (var items in BLL.BllIcon.GetAllIcon(""))
        //{
        //    noidung = noidung.Replace(items.KyTu, " <img src='../../Theme/Icon/" + items.Image + "'/>");
        //}
        return noidung;
    }

    public static string XulyTimkiem(string noidung)
    {
        string a = noidung;
        a = a.Replace("<", "");
        a = a.Replace(">", "");
        noidung = a;
        return noidung;
    }
    public static string MaNgauNhien_SoChu(int codeCount)
    {
        string allChar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
        string[] allCharArray = allChar.Split(',');
        string randomCode = "";
        int temp = -1;
        Random rand = new Random();
        for (int i = 0; i < codeCount; i++)
        {
            if (temp != -1)
            {
                rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
            }
            int t = rand.Next(36);
            if (temp != -1 && temp == t)
            {
                return MaNgauNhien_SoChu(codeCount);
            }
            temp = t;
            randomCode += allCharArray[t];
        }
        return randomCode;
    }
    private static string gMailAccount { get { return System.Configuration.ConfigurationManager.AppSettings["mail"]; } }
    private static string password { get { return System.Configuration.ConfigurationManager.AppSettings["mailpassword"]; } }

    public static string Domain { get { return System.Configuration.ConfigurationManager.AppSettings["domain"]; } }
    public static string Port { get { return System.Configuration.ConfigurationManager.AppSettings["port"]; } }
    public static void  sendmail(string EmailDangKy,string Subject, string Body,string NoiDung)
    {
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(gMailAccount);
            mail.To.Add(EmailDangKy);
            mail.Subject = Subject;
            mail.Body = Body + "<br/>";
            mail.Body +=NoiDung;
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.IsBodyHtml = true;
            SmtpClient mySmtpClient = new SmtpClient();
            mySmtpClient.Host = "smtp.gmail.com"; ;
            mySmtpClient.Credentials = new System.Net.NetworkCredential(gMailAccount, password);
            mySmtpClient.EnableSsl = true;
            mySmtpClient.Send(mail);
    }
    public static void sendmail1(string EmailDangKy, string Subject, string Body, string NoiDung,string MailAccount,string MK)
    {
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress(MailAccount);
        mail.To.Add(EmailDangKy);
        mail.Subject = Subject;
        mail.Body = Body + "<br/>";
        mail.Body += NoiDung;
        mail.BodyEncoding = System.Text.Encoding.UTF8;
        mail.IsBodyHtml = true;
        SmtpClient mySmtpClient = new SmtpClient();
        mySmtpClient.Host = "smtp.gmail.com"; ;
        mySmtpClient.Credentials = new System.Net.NetworkCredential(MailAccount, MK);
        mySmtpClient.EnableSsl = true;
        mySmtpClient.Send(mail);
    }
}